<template>
  <div class="knowlege-detail-container">
    <div class="title">{{ updateInfo.title }}</div>
    <div class="tips">
      <span>作者：</span>
      <span style="margin-right:20px;">{{ updateInfo.ownName }}</span>
      <span>日期：</span>
      <span>{{ formatDate(updateInfo.updateTime) }}</span>
    </div>
    <div class="detail-img">
      <!-- <video v-if="updateInfo.type === 'mp4' || updateInfo.type === 'MP4'" id="video" width="900" height="360"
        :src="$store.state.imgShowRoad + '/file/' + updateInfo.picPath" controls> </video> -->
      <img style="width:500px;height:300px;" :src="imgShowRoad + '/order/' + updateInfo.picPath"
        alt="" />
    </div>
    <div class="detail-content">
      <pre>{{ updateInfo.content }}</pre>
    </div>
    <el-input type="textarea" v-model="content" :rows="5"></el-input>
    <div style="margin-top:20px;display: flex;flex-direction: row;justify-content: flex-end">
      <el-button type="success" @click="handleComment">添加评论</el-button>
    </div>
    <div class="comment-container">
      <div class="comment-num">评论共{{ commentArray.length || 0 }}条</div>
      <div class="comment-item" v-for="(item, index) in commentArray" :key="index">
        <div>{{ item.content }}</div>
        <div class="comment-tips">
          <div style="margin-right:40px;">评论人：<span class="color6">{{ item.ownName }}</span></div>
          <div>评论时间：<span class="color6">{{ formatDateTime(item.createTime) }}</span></div>
        </div>
      </div>
    </div>
  </div>
</template>
  
<script lang="ts" setup>
import { useRoute } from 'vue-router'
import { GetKnowLedgeDetailAPI } from '../api/knowledge'
import { GetDiscussAPI } from '../api/discuss'
import { knowledegedetail } from '../store';
import { storeToRefs } from 'pinia';
import { AddDiscussAPI } from '../api/discuss'

const route = useRoute()  

function getKnowLedgeDetail(knowledgeId: string) {
  GetKnowLedgeDetailAPI({knowledgeId:knowledgeId})
  .then((res) => {
      if (res.code === 200 && res.msg !== null) {
        updateInfo.value = res.data

      }
      if (res.code === 100 && res.msg !== null) {
        ElMessage.error(res.msg)
      }

    })
}

onMounted(() => {
  getKnowLedgeDetail(route.params.knowledgeId as string)
  getDiscuss(route.params.knowledgeId as string)
})

function formatDateTime(createTime: string) {
  const date = new Date(createTime);
  return date.toLocaleString();
}
function formatDate(createdate: string) {
  const date = new Date(createdate);
  return date.toLocaleDateString();
}

const commentArray = ref([
  {
    content:'',
    ownName: '',
    createTime: '',
  }
])

const content = ref('')

const updateInfo = ref({
  title: '',
  ownName: '',
  updateTime: '',
  content: '',
  createTime: '',
  picPath: '',
})

function handleComment(){
  AddDiscussAPI({
    knowledgeId: route.params.knowledgeId as string,
    content: content.value,
    ownName: sessionStorage.getItem('ownName') as string,
  })
  .then((res) => {
      if (res.code === 200 && res.msg !== null) {
        ElMessage.success(res.msg)
        getDiscuss(route.params.knowledgeId as string)
      }
      if (res.code === 100 && res.msg !== null) {
        ElMessage.error(res.msg)
      }

    })
  
}

const main = knowledegedetail()
// 解构main里面的state和getters的数据，
// 使用storeToRefs解构才有响应式，响应式可以直接修改数据，不过这我只用来渲染
let { imgShowRoad } = storeToRefs(main)

function getDiscuss(knowledgeId : string){
  GetDiscussAPI({knowledgeId:knowledgeId})
  .then((res) => {
      if (res.code === 200 && res.msg !== null) {
        commentArray.value = res.data

      }
      if (res.code === 100 && res.msg !== null) {
        ElMessage.error(res.msg)
      }

    })
}
</script>
  
<style lang="less" scoped>
.knowlege-detail-container {
  width: 1100px;
  margin: 0 auto;
  background: #fff;
  min-height: 100%;
  padding: 10px 20px;
  margin-top: 10px;

  .title {
    font-size: 18px;
    text-align: center;
  }

  .tips {
    color: #999;
    display: flex;
    justify-content: flex-end;
    height: 30px;
    align-items: center;
  }

  .detail-img {
    display: flex;
    justify-content: center;
    margin-top: 50px;

    video {
      border: 1px solid #f2f2f2;
    }

    img {
      border-radius: 6px;
    }
  }

  // .detail-content{

  // }
  .comment-container {
    clear: both;
    margin-top: 10px;

    .comment-num {
      font-weight: bold;
    }

    .comment-item {
      border-bottom: 1px solid #f2f2f2;
      padding: 10px 20px;
      margin: 20px 0;
      word-break: break-all;

      .comment-tips {
        display: flex;
        flex-direction: row;
        justify-content: flex-end;

        .color6 {
          color: #666;
        }
      }
    }
  }
}</style>